package com.yxd.bdmapdemo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import java.util.Locale;
import java.util.UUID;

/**
 * Created by Administrator on 2017/5/19.
 */

public class SystemUtil {

    public static String getAppInfo() {
        try {
            String pkName = UIUtils.getContext().getPackageName();
            String versionName = UIUtils.getContext().getPackageManager().getPackageInfo(
                    pkName, 0).versionName;
            int versionCode = UIUtils.getContext().getPackageManager()
                    .getPackageInfo(pkName, 0).versionCode;
            return /*pkName + "   " + */versionName + "  " + versionCode;
        } catch (Exception e) {
        }
        return " ";
    }

    /**
     * 获取当前手机系统语言。
     *
     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
     */
    public static String getSystemLanguage() {
        return Locale.getDefault().getLanguage();
    }

    /**
     * 获取当前系统上的语言列表(Locale列表)
     *
     * @return 语言列表
     */
    public static Locale[] getSystemLanguageList() {
        return Locale.getAvailableLocales();
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return 手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return 手机厂商
     */
    public static String getDeviceBrand() {
        return android.os.Build.BRAND;
    }

    /**
     * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return 手机IMEI
     */
    public static String getIMEI(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
        if (tm != null) {
            L.d("IMEI -----------"+tm.getDeviceId());
            return tm.getDeviceId();
        }
        return null;
    }


    public static double getScreenPhysicalSize(Activity ctx) {
        DisplayMetrics dm = new DisplayMetrics();
        ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
        double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
        return diagonalPixels / (160 * dm.density);
    }

    public static String getDeviceId(String localMac, String androidId) {

        SharedPreferences sp = UIUtils.getContext().getSharedPreferences("device_info", Context.MODE_PRIVATE);
        String device_id = sp.getString("device_id", "");

        if (TextUtils.isEmpty(device_id)) {
            device_id = localMac.replace(":", "");// getLocalMac(context);

            if (TextUtils.isEmpty(device_id)) {
                device_id = androidId;
                if (TextUtils.isEmpty(device_id)) {
                    UUID uuid = UUID.randomUUID();
                    device_id = uuid.toString().replace("-", "");
                    // SharedPreferences sp = UIUtils.getContext().getSharedPreferences("device_id", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("device_id", "" + device_id);
                    editor.commit();
                }
            }
        }

        return device_id;
    }


    // Mac地址
    public static String getLocalMac(Context context) {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifi != null) {
            WifiInfo info = wifi.getConnectionInfo();
            if (wifi != null) {
                L.d("LocalMac -----------"+info.getMacAddress());
                return info.getMacAddress();
            }
        }
        return "";
    }

    // Android Id
    public static String getAndroidId(Context context) {
        String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (TextUtils.isEmpty(androidId)) {
            return "";
        }
        L.d("android -----------"+androidId);
        return androidId;
    }

}

